var express = require('express');
var query = require('./db');
var moment = require('moment');
var cartRouter = express.Router();

cartRouter.get('/carts/prosum', function (req, res, next) {
    query(`select sum(num) as num from cart where uid=${req.cookies.uid} and status=1`, function (err, result) {
        let obj={
            status:200,
            data:result
        }
        if(err){
            status=0;
            data=err;
            res.send(obj);
        }
        else{
            res.send(obj);
        }
    });
});

cartRouter.get('/getCartList', function (req, res, next) {
    query(`select * from cart where uid=${req.cookies.uid} and status=1`, function (err, result) {
        let obj={
            status:200,
            data:result
        }
        if(err){
            status=0;
            data=err;
            res.send(obj);
        }
        else{
            res.send(obj);
        }
    });
});

cartRouter.post('/addCarts',function(req,res,next){
    let datestr=moment(Date.now()).format('YYYY-MM-DD HH:mm:ss');
   let sql=`insert into cart(uid,pid,proname,proimg,proprice,prototalprice,num,status,createdtime,proselected) values 
   (${req.body.uid},${req.body.pid},'${req.body.proname}','${req.body.proimg}',${req.body.proprice},${req.body.prototalprice},
   ${req.body.num},1,'${datestr}',false)
   `
query(sql,function(err,result){
            let obj={
            status:200,
            data:result
        }
        if(err){
            status=0;
            data=err;
            res.send(obj);
        }
        else{
            res.send(obj);
        }
});

})

cartRouter.post('/upProSel',function(req,res,next){
   let sql=`
   update cart set proselected=${req.body.proselected} where cid=${req.body.cid}
   `
query(sql,function(err,result){
            let obj={
            status:200,
            data:result
        }
        if(err){
            status=0;
            data=err;
            res.send(obj);
        }
        else{
            res.send(obj);
        }
});

})


cartRouter.post('/upSelAll',function(req,res,next){
    let sql=`
    update cart set proselected=${req.body.proselected} where uid=${req.cookies.uid}
    `
 query(sql,function(err,result){
             let obj={
             status:200,
             data:result
         }
         if(err){
             status=0;
             data=err;
             res.send(obj);
         }
         else{
             res.send(obj);
         }
 });
 
 })

 cartRouter.post('/delCart',function(req,res,next){
    let sql=`
    delete from cart where cid=${req.body.cid}
    `
 query(sql,function(err,result){
             let obj={
             status:200,
             data:result
         }
         if(err){
             status=0;
             data=err;
             res.send(obj);
         }
         else{
             res.send(obj);
         }
 });
 
 })
 
 cartRouter.post('/upProNum',function(req,res,next){
    let sql=`
    update cart set num=${req.body.num} where cid=${req.body.cid}
    `
 query(sql,function(err,result){
             let obj={
             status:200,
             data:result
         }
         if(err){
             status=0;
             data=err;
             res.send(obj);
         }
         else{
             res.send(obj);
         }
 });
 
 })

module.exports = cartRouter;